<?php
/*
 * 手机客户端 企业注册
 * 2013-12-14
 */
include "inc/public_header.php";
/**/
$mobile  		= $_REQUEST["mobile"];			//手机号
$userpwd 		= $_REQUEST["userpwd"];			//密码md5
$vcode 			= $_REQUEST["vcode"];			//验证码
$isCompany 		= $_REQUEST["isCompany"];		//是否是企业
$companyName	= $_REQUEST["companyName"];		//企业名称
$legal			= $_REQUEST["legal"];			//企业法人
$manager 		= $_REQUEST["manager"];			//经办人
$managerTel 	= $_REQUEST["managerTel"];		//经办人联系电话
$license 		= $_REQUEST["license"];			//营业执照

/*
	//输入手机号
if(!isset($mobile)){
	echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"请输入手机号"));
	die;
}
else if($mobile.length != 11 || parseInt($mobile) != $mobile){
	echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"请输入正确格式的手机号码！"));
	die;
}
else{
	//手机号已注册
	$sql=" 	select uid,uuid
			from user_base
			where mobile = '".$mobile."'";
	$db->query($sql);
	if($db->next_record()){
		echo json_encode(array("success"=>"0","errCode"=>"1","msg"=>"手机号已注册"));
		die;
	}
	
}

//输入密码，确认密码，2个密码相同
if(!isset($userpwd)){
	
	echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"请输入登录密码"));
	die;
}
*/

//手机号已注册
$sql="	select uid,uuid
		from user_base
		where mobile = '".$mobile."'";
$db->query($sql);
if($db->next_record()){
	echo json_encode(array("success"=>"0","errCode"=>"1","msg"=>"手机号已注册"));
	die;
}

//验证码是否过期，未过期判断验证码是否正确
$sql="select mobile,vcode,counts,cuts
	from user_regvcode
	where mobile = '".$mobile."' and cuts > UNIX_TIMESTAMP()-1800";
$db->query($sql);
if(!$db->next_record()){
	echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"验证码过期"));
	die;
}else if($vcode!=$db->f("vcode")||!$db->f("vcode")){
	echo json_encode(array("success"=>"0","errCode"=>"0","msg"=>"验证码输入错误"));
	die;
}

/*
//输入验证码
if(!isset($vcode)){
	echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"请输入手机短信验证码"));
	die;
}else{

$sql="select mobile,vcode,counts,cuts
		from user_regvcode
		where mobile = '".$mobile."' and cuts > UNIX_TIMESTAMP()-1800";
	$db->query($sql);
	//验证码是否过期，未过期判断验证码是否正确
	if($db->next_record()){
		echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"验证码过期"));
		die;
	}else if($vcode!=$db->f("vcode")||!$db->f("vcode")){
		echo json_encode(array("success"=>"0","errCode"=>"0","msg"=>"验证码输入错误"));
		die;
	}
}

//是否是公司注册
if(!isset($isCompany)){
		echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"请输入您的性别"));
		die;
	}
	
	//是否是企业名称
	if(!isset($companyName)){
		echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"请输入您的性别"));
		die;
	}
	//是否是企业法人
	if(!isset($legal)){
		echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"请输入您的生日"));
		die;
	}
	//是否是经办人
	if(!isset($manager)){
		echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"请选择您的证件类型"));
		die;
	}
	//是否是经办人电话
	if(!isset($managerTel)){
		echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"请输入您的证件号码"));
		die;
	}
*/
//是否是营业执照

$uploadOK = '0';
if($_FILES['license']['tmp_name']){
	$tmpPath = $_FILES['license']['tmp_name'];
	$oldFileName = $_FILES['license']['name'];
	$extName = explode(".",$oldFileName);
	$extName = $extName[count($extName)-1];
	$newFileName = date("YmdHms").".".$extName;
	if(strtolower($extName)!="jpg" && strtolower($extName)!="jpeg"){
		echo json_encode(array("success"=>"0","errCode"=>"6","msg"=>"上传的图片只能是jpg/jpeg格式(".$oldFileName.")"));
		die;
	}
	$uploaddir = $_SERVER["DOCUMENT_ROOT"]."/img1/uploads/business_licence/";
	if(move_uploaded_file($tmpPath,$uploaddir.$newFileName)){
		/*function create_uuid($prefix = ""){    //可以指定前缀
			$str = md5(uniqid(mt_rand(), true));   
			$uuid  = substr($str,0,8) . '-';   
			$uuid .= substr($str,8,4) . '-';   
			$uuid .= substr($str,12,4) . '-';   
			$uuid .= substr($str,16,4) . '-';   
			$uuid .= substr($str,20,12);   
			//return $prefix . $uuid;
		}*/
		
		$str = md5(uniqid(mt_rand(), true));   
			$uuid  = substr($str,0,8) . '-';   
			$uuid .= substr($str,8,4) . '-';   
			$uuid .= substr($str,12,4) . '-';   
			$uuid .= substr($str,16,16);   
	
		$xx_sql="insert into user_base set uuid='".$uuid."',muuid='".$uuid."',mobile='".$mobile."',mobile_checked='1',userpwd='".$userpwd."',from_type='mobile',ustatus=1,reg_dt=now(),now_login_time=now(),isCompany='".$isCompany."',companyname='".$companyName."',business_entity='".$legal."',gestor='".$manager."',gestor_phone='".$managerTel."',business_licence='".$newFileName."'";
		$db->query($xx_sql);
		$reUrl=$uploaddir.$newFileName;
		$uploadOK  = '1';

		$x_sql="select uid,uuid from user_base where mobile='".$mobile."'";
	$db2->query($x_sql);
	while($db2->next_record()){
		$uuid1=$db2->f("uuid");
		$uid=$db2->f("uid");
		}
		$zs_sql="insert into user_glftcredit(UID,glftlevel,credit,senddt,isEnabled,rundt)
          value('".$uid."',1,100,now(),1,now())";
		  $db->query($zs_sql);
		  
	}
}

if($uploadOK==1){
	
		echo json_encode(array(
		"success"=>"1"
		,"uuid"=>(string)$uuid1
		));
	}
die;


?>